package com.itsm.xkitsm.liferoot.life.weather;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.itsm.xkitsm.liferoot.MainAct;

public class HttpDownloader {
	// 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文本当中的内容
	// 1.创建一个URL对象
	// 2.通过URL对象,创建一个HttpURLConnection对象
	// 3.得到InputStream
	// 4.从InputStream当中读取数据
	private URL url;
	public static String URL_DOWNLOAD = "url";
	public static int WHAT_DOWNLOADFINISH = 0x100;
	public static String ACTION = "com.fenghuo.service.project1206_001";
	private String temp = null;
	public static int URLSTR = 2;
	public static int URLSTR_CITYINFO = 1;
	public int currentIndex;

	public int getCurrentIndex() {
		return currentIndex;
	}

	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}

	public String download(String urlStr) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader buffer = null;
		try {
			url = new URL(urlStr);
			Log.i("ulr==",url+"");
			HttpURLConnection urlConn = (HttpURLConnection) url
					.openConnection();
			buffer = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));

			if (buffer!=null){
				while ((line = buffer.readLine()) != null) {
					sb.append(line);
					Message message = Message.obtain();
					if (urlStr.contains("sk")) {
						message.arg1 = URLSTR_CITYINFO;
					} else {
						message.arg1 = URLSTR;
					}

					message.obj = sb.toString();
					message.what = WHAT_DOWNLOADFINISH;
					message.arg2 = getCurrentIndex();
//			 MainAct.handler.sendMessage(message);
//			Log.e("Httpdownload:message.arg1:", message.arg1 + "");

					MainAct.hand.sendMessage(message);
					buffer.close();

				}
			}


		} catch (Exception e) {
			e.printStackTrace();
		}
//		finally {
//			try {
//				if(buffer!=null){
//				}
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
		Log.e("httpDownload" + urlStr, sb.toString());
		return sb.toString();

	}

	// ����¼�ˢ��ʱ����
	public String download_click(String urlStr) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader buffer = null;
		try {
			url = new URL(urlStr);
			HttpURLConnection urlConn = (HttpURLConnection) url
					.openConnection();
			buffer = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
			while ((line = buffer.readLine()) != null) {
				sb.append(line);
			}
			Message message = Message.obtain();
			if (urlStr.contains("sk")) {
				message.arg1 = URLSTR_CITYINFO;
			} else {
				message.arg1 = URLSTR;
			}

			message.obj = sb.toString();
			message.what = WHAT_DOWNLOADFINISH;
			message.arg2 = getCurrentIndex();

			// MainActivity_spring.handler.sendMessage(message);

//			Log.e("Httpdownload:message.arg1:", message.arg1 + "");
			// AddCityViewActivity.hand.sendMessage(message);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != buffer) {
					buffer.close();
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Log.e("httpDownload" + urlStr, sb.toString());
		return sb.toString();

	}
}
